package com.link.play.fit.ai.ui.main.fragment.control

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.link.play.fit.ai.bean.AdjustGoalsData
import com.link.play.fit.ai.bean.BaseScanFoodBean
import com.link.play.fit.ai.bean.CalControlReqData
import com.link.play.fit.ai.bean.RecentlyEatenData
import com.link.play.fit.ai.bean.UserGuide
import com.link.play.fit.ai.ext.d
import com.link.play.fit.ai.utils.ApiResult
import com.link.play.fit.ai.utils.ApiService
import com.link.play.fit.ai.utils.HttpResponse.safeApiCall
import com.link.play.fit.ai.utils.RetrofitClient
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

class ResultViewModel: ViewModel() {
    private val apiService = RetrofitClient.createService(ApiService::class.java)

    val foodBeanLiveData: SharedFlow<BaseScanFoodBean> get() = _foodBeanLiveData
    private val _foodBeanLiveData = MutableSharedFlow<BaseScanFoodBean>(replay = 0)



    /**
     * AI减肥饮食运动计划接口
     */
    fun getCalControl(data: CalControlReqData) {
        "getCalControl  $data".d()
        viewModelScope.launch {
            when (val result = safeApiCall {
                apiService.calControl(data)
            }) {
                is ApiResult.Success -> {
                    _foodBeanLiveData.emit(
                        BaseScanFoodBean(
                            status = 200,
                            calControlData = result.data
                        )
                    )
                }
                is ApiResult.BusinessError -> {
                    _foodBeanLiveData.emit(
                        BaseScanFoodBean(
                            status = 200,
                            msg = result.message
                        )
                    )
                }

                is ApiResult.HttpError -> {
                    _foodBeanLiveData.emit(
                        BaseScanFoodBean(
                            status = 200,
                            msg = result.message
                        )
                    )
                }

                is ApiResult.NetworkError -> {
                    _foodBeanLiveData.emit(
                        BaseScanFoodBean(
                            status = 200,
                            msg = "Network connection failed"
                        )
                    )
                }
            }
        }
    }

}